/* Copyright (c) 2006 YourNameHere
   See the file LICENSE.txt for licensing information. */


function nhn_layoutdialog_link_button_clicked(aEvent){

    
    var param = window.arguments[0];
    //param.showAlert(aEvent);
    
    //window.close();
}

function nhn_layoutdialog_index_change_clicked(aEvent){
    var d = document.getElementById("mychangedeck");
    var index = d.getAttribute("selectedIndex");
    index += 1;
    if(index > 2){
        index = 0;
    }
    d.setAttribute("selectedIndex", index+"");
}

function HttpActionHandler(){
    this._request = new XMLHttpRequest();
    
    this._request.onload = function(aEvent){httpNhnHandler.onLoadHandler(aEvent);};
    this._request.onprogress = function(aEvent){httpNhnHandler.onProgressHandler(aEvent);};
    this._request.onerror = function(aEvent){httpNhnHandler.onErrorHandler(aEvent);};
    this._request.onreadystatechange = function(aEvent){httpNhnHandler.onReadyState(aEvent);};
}


HttpActionHandler.prototype.startHttp = function(){
    
    this._request.open("GET", "http://zhangmenshiting2.baidu.com/data2/music/1481847/1481847.mp3?xcode=86e05a81501126674142dc87def856f4&r=1336630649", true);
    this._request.send(null);
    
    //if(this._request.status == 200 ){
    //    
    //    var header = this._request.getResponseHeader("Date");
    //    //alert(this._request.responseType);
    //    //alert(this._request.responseText);
    //    var headers = this._request.getAllResponseHeaders();
    //    //alert(headers);
    //}
    //else{
    //    alert("error with code:" + request.status);
    //}
}

HttpActionHandler.prototype.callParentWindowFunction = function(){
    var w = window.opener;
    if(w){
        w.nhnJsStudy.testInOtherDlg();    
    }
    else{
        alert("null");
    }
}

HttpActionHandler.prototype.onLoadHandler = function(aEvent){
    // write file
    if(aEvent.target.status != 200){
        var lb = document.getElementById("httpProgressLabelId");
        lb.setAttribute("download failure");
        return;    
    }
    
    var file = FileIO.open("~/1.txt");
    if(!file.exists()){
        FileIO.create(file);
    }
    alert("content type:" + aEvent.target.getResponseHeader("Content-Type"));
    
    var str = aEvent.target.response;
    //alert(str.length);
    
    FileIO.write(file, "hello world");
}

HttpActionHandler.prototype.onErrorHandler = function(aEvent){
    alert("Error with code:" + aEvent.target.status);
}

HttpActionHandler.prototype.onProgressHandler = function(aEvent){
    var lb = document.getElementById("httpProgressLabelId");
    var p = 0;
    if(aEvent.lengthComputable){
        
        p = (aEvent.loaded/aEvent.total) * 100;
    }

    lb.setAttribute("value", "Progress is: " + p + "%");

}

HttpActionHandler.prototype.onReadyState = function(aEvent){
    //alert(aEvent.target.readyState);
}
